<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/28 0028
 * Time: 下午 6:13
 */
namespace app\api\service;

use app\admin\model\Couponuse;
use app\common\BaseHelper;
use app\common\model\Useraccountdetails;
use app\common\service\TemplateMessage;
use think\Cache;
use think\Db;
use think\Exception;
use app\admin\model\Orderoffline as OrderModel;
use think\Loader;
use think\Log;

Loader::import('WxPay.WxPay',EXTEND_PATH,'.Api.php');

/**
 * 处理微信支付回调
 * Class WxOffNotify
 * @package app\api\service
 */
class WxOffNotify extends \WxPayNotify{

    //<xml>
    //<appid><![CDATA[wx2421b1c4370ec43b]]></appid>
    //<attach><![CDATA[支付测试]]></attach>
    //<bank_type><![CDATA[CFT]]></bank_type>
    //<fee_type><![CDATA[CNY]]></fee_type>
    //<is_subscribe><![CDATA[Y]]></is_subscribe>
    //<mch_id><![CDATA[10000100]]></mch_id>
    //<nonce_str><![CDATA[5d2b6c2a8db53831f7eda20af46e531c]]></nonce_str>
    //<openid><![CDATA[oUpF8uMEb4qRXf22hE3X68TekukE]]></openid>
    //<out_trade_no><![CDATA[1409811653]]></out_trade_no>
    //<result_code><![CDATA[SUCCESS]]></result_code>
    //<return_code><![CDATA[SUCCESS]]></return_code>
    //<sign><![CDATA[B552ED6B279343CB493C5DD0D78AB241]]></sign>
    //<sub_mch_id><![CDATA[10000100]]></sub_mch_id>
    //<time_end><![CDATA[20140903131540]]></time_end>
    //<total_fee>1</total_fee>
    //<trade_type><![CDATA[JSAPI]]></trade_type>
    //<transaction_id><![CDATA[1004400740201409030005092168]]></transaction_id>
    //</xml>
    public function NotifyProcess($data, &$msg){
        if($data['result_code'] == 'SUCCESS'){
            $orderNo = $data['out_trade_no'];
            $paytime = self::timeFormat($data['time_end']);
            Db::startTrans();
            try{
                $order = OrderModel::where('id',$orderNo)->find();

                //写入微信支付单号
                $transaction_id = $data['transaction_id'];
                OrderModel::where('id',$orderNo)->update(['transaction_Id'=>$transaction_id,'payDate'=>$paytime,'payMethod'=>0,'status'=>2]);

                //如果使用了优惠券则要让优惠券变为已使用状态
                if($order->couponId){
                    $Couponuse = Couponuse::where(['couponId'=>$order->couponId,'userId'=>$order->userId,'storeId'=>$order->storeId,'status'=>0])->update(['status'=>1]);
                }
                $Useraccountdetails = new Useraccountdetails();
                $Useraccountdetails->insertRecord($order,Useraccountdetails::TYPE_6,Useraccountdetails::MODEL_1);
                Db::commit();
                //发送模板消息
                if(Cache::get('ORDERID') != $order->id){
                    TemplateMessage::paysendmessage($order->id);
                    Cache::set('ORDERID',$order->id);
                }
                return true;
            }catch (Exception $ex){
                Db::rollback();
                Log::record($ex);
                return false;
            }
        }else{
            return true;
        }
    }


    public static function timeFormat($time){
        $paytime = substr($time,0,4).'-';
        $paytime .= substr($time,4,2).'-';
        $paytime .= substr($time,6,2);
        $paytime .= ' ';
        $paytime .= substr($time,8,2).':';
        $paytime .= substr($time,10,2).':';
        $paytime .= substr($time,12,2);
        $paytime = strtotime($paytime);
        return $paytime;
    }

}